package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @author DBX
 * @effect 利用反射机制操作方法
 * @date 2021/09/15/11:31
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception{
        User user = new User();
        Class cls = Class.forName("reflect.User");
        Object o = cls.newInstance();

        Method method = cls.getMethod("sayHello");
        method.invoke(o);

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = sc.nextLine();
        System.out.println("请输入方法名：");
        String methodName = sc.nextLine();

        Class cls1 = Class.forName(className);
        Object o1 = cls1.newInstance();
        Method method1 = cls.getMethod(methodName);
        method1.invoke(o);
    }
}
